package talk.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.talk.debby.talk.R;

import java.util.ArrayList;
import java.util.List;

import base.BaseActivity;
import talk.Bean.Course;
import talk.dapter.ItemAdapter;
import talk.Utils.dataUtil;
import talk.dapter.MyViewPagerAdapter;
import talk.listener.OnItemClickListener;

/**
 * Created by debby on 2018/5/22.
 * 描述:
 */

public class PhotoViewActivity extends BaseActivity {

    private PagerAdapter pagerAdapter;
    private ViewPager viewpager;
    //列表
    private RecyclerView rv_data;
    private List<Course> courseList = new ArrayList<>();
    //
    private RelativeLayout rl_photo_view;
    private LinearLayout ll_main;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_view);
        initView();
        initData();
    }

    private void initView() {
        rv_data = findViewById(R.id.rv_data);
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        rl_photo_view = findViewById(R.id.rl_photo_view);
        ll_main = findViewById(R.id.ll_main);
        //图片预览
        viewpager = findViewById(R.id.vp_viewpager);
        viewpager.setOffscreenPageLimit(2);
        //返回键
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPhotoView(false);
            }
        });
    }

    private void initData() {
        courseList.add(dataUtil.addList1("第一章 质点运动学"));
        courseList.add(dataUtil.addList1("第二章 动量与动量守恒"));
        courseList.add(dataUtil.addList1("第三章 机械能 机械能守恒"));
        courseList.add(dataUtil.addList4("第四章 刚体的转动"));
        courseList.add(dataUtil.addList4("第五章 相对论"));
        ItemAdapter adapter = new ItemAdapter(this, courseList, new OnItemClickListener() {
            @Override
            public void onSelect(List<String> list) {
                refreshData(list);
                showPhotoView(true);
            }
        });
        rv_data.setAdapter(adapter);
    }

    private void showPhotoView(Boolean show) {
        if (show) {
            rl_photo_view.setVisibility(View.VISIBLE);
            ll_main.setVisibility(View.GONE);
        } else {
            rl_photo_view.setVisibility(View.GONE);
            ll_main.setVisibility(View.VISIBLE);
        }
    }

    private void refreshData(List<String> images) {
        pagerAdapter = new MyViewPagerAdapter(this, images);
        viewpager.setAdapter(pagerAdapter);
        viewpager.setCurrentItem(0);
    }

    @Override
    public void onBackPressed() {
        if (rl_photo_view.getVisibility() == View.VISIBLE) {
            showPhotoView(false);
        } else {
            super.onBackPressed();
        }
    }
}
